package com.mfangsoft.zhuangjialong.common.controller;

import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.nio.file.Path;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Hashtable;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.mfangsoft.zhuangjialong.common.model.ResponseMessage;
import com.mfangsoft.zhuangjialong.common.utils.Base64;
import com.mfangsoft.zhuangjialong.common.utils.PropUtis;
import com.mfangsoft.zhuangjialong.common.utils.SysConstant;
@Controller
@RequestMapping("/common")
public class VRController {
	
	
	
	@RequestMapping(value = "/vr/updoad", method = RequestMethod.POST)
	@ResponseBody
	public ResponseMessage<String>  uploadVRFile(@RequestParam("file") MultipartFile file){
		
		Long time = System.currentTimeMillis();

		java.text.SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");

		String exname = FilenameUtils.getExtension(file.getOriginalFilename());

		String path = PropUtis.getValue("vr.file.path") + "/" + time;
		
		File filepath = new File(path);
		if (!filepath.exists()) {

			filepath.mkdirs();
		}

		ResponseMessage<String> message = new ResponseMessage<>();

		try {
			File write = new File(filepath.getPath() + "/" + time + "." + exname);
			write.createNewFile();
			FileUtils.writeByteArrayToFile(write, file.getBytes());
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			String resultPath = PropUtis.getValue("vr.file.url") + "/" + time + "/" + time + "."
					+ exname;
			
			this.createCodePng(PropUtis.getValue("vr.file.url")+"/"+time+"/vtour/tour.html", path);
			this.exec(write.getPath());
			message.setResult(PropUtis.getValue("vr.file.url")+"/"+time+"/code.png");
			
			
		} catch (IOException | WriterException | InterruptedException e) {

			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
			message.setResult(e.getMessage());
			e.printStackTrace();
		}

		return message;
		
	}
	
	
	
	@RequestMapping(value = "/vr/updoadstring", method = RequestMethod.POST)
	@ResponseBody
	public ResponseMessage<String>  uploadVRFile(@RequestBody Map<String,Object> map){
		
		Long time = System.currentTimeMillis();

		java.text.SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");

		String exname = "jpg";

		String path = PropUtis.getValue("vr.file.path") + "/" + time;
		
		File filepath = new File(path);
		if (!filepath.exists()) {

			filepath.mkdirs();
		}
		
		byte[] imgByte = Base64.decode(map.get("ImageData").toString());
		

		ResponseMessage<String> message = new ResponseMessage<>();

		try {
			File write = new File(filepath.getPath() + "/" + time + "." + exname);
			write.createNewFile();
			FileUtils.writeByteArrayToFile(write, imgByte);
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			String resultPath = PropUtis.getValue("vr.file.url") + "/" + time + "/" + time + "."
					+ exname;
			
			this.createCodePng(PropUtis.getValue("vr.file.url")+"/"+time+"/vtour/tour.html", path);
			this.exec(write.getPath());
			message.setResult(PropUtis.getValue("vr.file.url")+"/"+time+"/code.png");
			
			
		} catch (IOException | WriterException | InterruptedException e) {

			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
			message.setResult(e.getMessage());
			e.printStackTrace();
		}

		return message;
		
	}
	
	private  void  exec(String path) throws IOException, InterruptedException{
		
		String exec="/root/soft/krpano-1.19-pr8/krpanotools makepano -config=templates/vtour-normal.config "+path;
		
		Process  process  =Runtime.getRuntime().exec(exec);
		
		
        InputStreamReader ir = new InputStreamReader(process.getInputStream());

        LineNumberReader input = new LineNumberReader(ir);



        String line;

        while ((line = input.readLine()) != null) {

            System.out.println(line);

        }
		
		
	}
	
	
	private void createCodePng(String text,String path) throws WriterException, IOException{
		int width=100;
		int height=100;
		String format="png";
		Hashtable hints=new Hashtable();
		hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
		hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
		hints.put(EncodeHintType.MARGIN, 2);
		
		BitMatrix bitMatrix=new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height,hints);
		Path file=new File(path+"/code.png").toPath();
		MatrixToImageWriter.writeToPath(bitMatrix, format, file);
		
	}
	
	@RequestMapping(value = "/vr/ex", method = RequestMethod.GET)
	@ResponseBody
	public ResponseMessage<String>  exVRFile() throws IOException{
		
		ResponseMessage<String> message = new ResponseMessage<>();
		String exec="/root/soft/krpano-1.19-pr8/krpanotools makepano -config=templates/vtour-normal.config /root/soft/file/upload/vr/1488344545457/1488344545457.jpg";
		
		Process  process  =Runtime.getRuntime().exec(exec);
		
        InputStreamReader ir = new InputStreamReader(process.getInputStream());

        LineNumberReader input = new LineNumberReader(ir);

        String line=null;

        StringBuffer  buffer = new StringBuffer();
        while ((line = input.readLine()) != null) {

            buffer.append(line);

        }
        
        message.setResult(buffer.toString());
        return message;
	}
	public static void main(String[] args) {
		
		String exec="\"/root/soft/krpano-1.19-pr8/krpanotools\" makepano -config=templates/vtour-normal.config ";
		
		System.out.println(exec);
	}

}
